#include"SQ.h"

void QueueInit(Que* pq)
{
	pq->head = NULL;
	pq->tail= NULL;
	pq->size = 0;


}
void QueueDestroy(Que* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (!QueueNode(pq))
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;

	}
	pq->head = pq->tail = NULL;
	pq->size = 0;


}
void QueuePush(Que* pq, QDataType x)
{
	assert(pq);
	QNode* temp = (QNode*)malloc(sizeof(QNode));
	if (temp == NULL)
	{
		perror("malloc");
		return;

	}
	temp->data = x;
	temp->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = temp;
	}
	else
	{
		pq->tail->next = temp;
		pq->tail = temp;


	}
	++pq->size;


}
void QueuePop(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->head->next=NULL)

	{
		free(pq->head);
		pq->head = NULL;
		pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;

	}
	pq->size--;



}
QDataType QueueFront(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
    return pq->head->data;


}
QDataType QueueBack(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}
bool QueueEmpty(Que* pq)
{
	assert(pq);
	return pq->size == 0;
}
int QueueSize(Que* pq)
{

	assert(pq);
	assert(pq->size > 0);
	return pq->size;
}
